package com.molichuxing.services.business.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.request.create.CarLicencePlateCreateDto;
import com.molichuxing.services.business.dto.request.modify.CarLicencePlateModifyDto;
import com.molichuxing.services.business.dto.response.CarLicencePlateDetailDto;
import com.molichuxing.services.business.dto.response.CarLicencePlateDto;
import com.molichuxing.services.business.service.CarLicencePlateBizService;
import com.molichuxing.services.exception.VehicleExceptionCodeEnum;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.LicencePlateDto;
import com.molichuxing.services.infrastructure.dto.response.YearlyInspectionDto;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.infrastructure.service.LicencePlateService;
import com.molichuxing.services.infrastructure.service.YearlyInspectService;
import com.molichuxing.services.property.CarLicencePlateStatusEnum;
import com.molichuxing.services.property.CarsOperateStatusEnum;
import com.molichuxing.services.utils.VehicleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 车牌列表服务实现类
 * @author xuelin yi
 */
@Service("carLicencePlateBizService")
public class CarLicencePlateBizServiceImpl implements CarLicencePlateBizService {

	private static final Logger log = LoggerFactory.getLogger(CarLicencePlateBizServiceImpl.class);

	@Resource
	private CarService carService;

	@Resource
	private LicencePlateService licencePlateService;

	@Resource
	private YearlyInspectService yearlyInspectService;

	@Override
	public CarLicencePlateDetailDto getLicencePlate(int carId) throws Exception{
		log.info("根据车辆id获取车牌信息 carId ： ["+ carId +"]");
		CarDto carDto = carService.getCarById(carId);
		if(null == carDto) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_EXIST.getCode(), VehicleExceptionCodeEnum.NOT_EXIST.getMessage());
		}
		LicencePlateDto licencePlateDto = licencePlateService.getLicensePlateByCarId(carId);
		/*if(null == licencePlateDto) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_EXIST.getCode(), VehicleExceptionCodeEnum.NOT_EXIST.getMessage());
		}*/
		YearlyInspectionDto yearlyInspectionDto = yearlyInspectService.getYearlyInspectionByCarId(carId);
		/*if(null == yearlyInspectionDto) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_EXIST.getCode(), VehicleExceptionCodeEnum.NOT_EXIST.getMessage());
		}*/
		return Convert.toCarLicencePlateDetailBeanDto(carDto, licencePlateDto, yearlyInspectionDto);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public int createLicencePlate(CarLicencePlateCreateDto createDto) throws Exception {
		if(!VehicleUtil.verificationLicencePlate(createDto.getLicencePlate())) {
			throw new BizException(VehicleExceptionCodeEnum.CAR_LICENCE_PLATE_ERROR.getCode(), VehicleExceptionCodeEnum.CAR_LICENCE_PLATE_ERROR.getMessage());
		}
		CarDto carDto = carService.getCarById(createDto.getCarId());
		if(carDto == null) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_EXIST.getCode(), VehicleExceptionCodeEnum.NOT_EXIST.getMessage());
		}
		if(carDto.getIsHavePlate()) {
			throw new BizException(VehicleExceptionCodeEnum.ALREADY_LICENCE_PALTE.getCode(), VehicleExceptionCodeEnum.ALREADY_LICENCE_PALTE.getMessage());
		}
		if(null == carDto.getStockId()) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_IN_STOCK.getCode(), VehicleExceptionCodeEnum.NOT_IN_STOCK.getMessage());

		}
		CarDto carLicenseDto = carService.getByVinLicencePlate(null, createDto.getLicencePlate());
		if(carLicenseDto != null) {
			throw new BizException(VehicleExceptionCodeEnum.LICENSE_PLATE_REPET.getCode(), VehicleExceptionCodeEnum.LICENSE_PLATE_REPET.getMessage());
		}
		int createLicenceResult = licencePlateService.createLicensePlate(Convert.toCarLicencePlateDetailDto(createDto));
		if(createLicenceResult <= 0){
			return createLicenceResult;
		}
		int createYearlyResult = yearlyInspectService.createYearlyInspection(Convert.toYearlyInspectionDto(createDto));
		if(createYearlyResult <= 0){
			return createYearlyResult;
		}
		int modifyLicencePlate = carService.modifyLicencePlateById(createDto.getCarId(), createDto.getLicencePlate(), createDto.getSubcompanyId());
		if(carDto.getIsHavePlate()&&carDto.getIsHaveCommercial()&&carDto.getIsHaveCompulsory()) {
			carService.modifyStatusById(createDto.getCarId(), CarsOperateStatusEnum.FOR_RENT);
		}
		return modifyLicencePlate;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public int modifyLicencePlate(CarLicencePlateModifyDto modifyDto) {
		CarDto carDto = carService.getCarById(modifyDto.getCarId());
		if(null == carDto) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_EXIST.getCode(), VehicleExceptionCodeEnum.NOT_EXIST.getMessage());
		}
		if(!carDto.getIsInStock()) {
			throw new BizException(VehicleExceptionCodeEnum.NOT_IN_STOCK.getCode(), VehicleExceptionCodeEnum.NOT_IN_STOCK.getMessage());
		}
		int result = yearlyInspectService.modifyYearlyInspection(Convert.toCarLicencePlateModifyDto(modifyDto));
		if(result <= 0){
			return result;
		}
		return licencePlateService.modifyLicensePlate(Convert.toLicencePlateModifyDto(modifyDto));
	}
}
